package com.cetc.baidumaptest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;

/**
 * Created by Young on 2017/1/12.
 */

public class TwoPointRectangle extends AppCompatActivity{
    MapView mMapView = null;
    BaiduMap mBaiduMap = null;
    Integer  clickNumber = 0;
    TextView txt1=null;
    TextView txt2=null;
    OverlayOptions ooPolyline;
    Polyline mPolyline;
    ArrayList<LatLng> line = new ArrayList<>();
    LatLng xPoint ;
    LatLng yPoint ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_line);
        txt1 = (TextView) findViewById(R.id.txt_point_1);
        txt2 = (TextView) findViewById(R.id.txt_point_2);
        mMapView = (MapView) findViewById(R.id.mapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            public void onMapClick(LatLng point) {
                //在此处理点击事件
                OverlayOptions ooDot = new DotOptions().center(point).radius(10)
                        .color(0xFF00CD00);
                mBaiduMap.addOverlay(ooDot);
                Toast.makeText(getApplicationContext(), "地图被点击", Toast.LENGTH_SHORT).show();
                clickNumber = clickNumber + 1;
                if (clickNumber == 1) {
                    line.add(0,point);
                    txt1.setText("纬度" + point.latitude + " 经度" + point.longitude);
                    xPoint = new LatLng(point.latitude,0);
                    yPoint = new LatLng(0,point.longitude);
                } else if (clickNumber == 2) {
                    txt2.setText("纬度" + point.latitude + " 经度" + point.longitude);
                    xPoint = new LatLng(xPoint.latitude,point.longitude);
                    yPoint = new LatLng(point.latitude,yPoint.longitude);
                    line.add(1,xPoint);
                    line.add(2,point);
                    line.add(3,yPoint);
                    line.add(4,line.get(0));
                    //画线
                    ooPolyline = new PolylineOptions().width(15).points(line);
                    mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);
                    clickNumber = 0;
                    line.clear();
                }

            }

            public boolean onMapPoiClick(MapPoi poi) {
                //在此处理底图标注点击事件
                return false;
            }
        });
    }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
            mMapView.onDestroy();
        }
        @Override
        protected void onResume() {
            super.onResume();
            //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
            mMapView.onResume();
        }
        @Override
        protected void onPause() {
            super.onPause();
            //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
            mMapView.onPause();
        }
}
